using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_HO_ManagementBranchesOnRoute : System.Web.UI.Page
{
    func fn = new func();
    string rID = null;
    string bID = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = fn.GETDATASET("select * from Routes order by StartPoint", "routes");
            foreach (DataRow dr in ds.Tables["routes"].Rows)
            {
                ListItem l = new ListItem(dr["StartPoint"].ToString() +" -- "+ dr["EndPoint"].ToString(), dr["RouteID"].ToString(), true);
                ddlRoutes.Items.Add(l);
            }
        }
        //bat su kien action
        if (!IsPostBack)
        {
            if (Request.QueryString["act"] != null)
            {

                switch (Request.QueryString["act"].ToString())
                {
                    case "edit":

                        if (Request.QueryString["rID"] != null && Request.QueryString["bID"] != null)
                        {
                            rID = Request.QueryString["rID"].ToString();
                            bID = Request.QueryString["bID"].ToString();

                            if (fn.CHECKEXIST("select * from BranchesOnRoute where RouteID='" + rID + "' and BranchID='" + bID + "'") == true)
                            {
                                DataRow dr = fn.GETDATAROWS("SELECT Branches.BranchID, Branches.BranchName, Branches.Location, Branches.Phone, Branches.CreatedDate, Branches.Status, BranchesOnRoute.RouteID, BranchesOnRoute.BranchID AS Expr1, BranchesOnRoute.Number FROM Branches INNER JOIN BranchesOnRoute ON Branches.BranchID = BranchesOnRoute.BranchID where BranchesOnRoute.RouteID='" + rID + "' and BranchesOnRoute.BranchID='" + bID + "'");
                                //load info branch on route
                                lblBranchesName.Text = dr["BranchName"].ToString();
                                txtNumberEdit.Text = dr["Number"].ToString();
                                lblRoutID.Text = rID;
                                lblBrachID.Text = bID;
                                pnEdit.Visible = true;
                            }
                            else
                            {
                                lblThongbao.Text = "Not Existing Branches on Routes";
                            }
                        }
                        break;
                    case"del":
                        if (Request.QueryString["rID"] != null && Request.QueryString["bID"] != null && Request.QueryString["number"]!=null)
                        {
                            string sql = "select * from BranchesOnRoute where RouteID='" + Request.QueryString["rID"].ToString() + "' and BranchID='" + Request.QueryString["bID"].ToString() +"'";
                            if (fn.CHECKEXIST(sql) == true)
                            {
                                  sql = "delete BranchesOnRoute where RouteID='" + Request.QueryString["rID"].ToString() +"' and BranchID='" + Request.QueryString["bID"].ToString() +"' and Number='" + Request.QueryString["number"].ToString() +"'";
                                if (fn.EXECQUERY(sql) > 0)
                                {
                                    lblThongbao.Text = "Deleted";
                                }
                                else {
                                    lblThongbao.Text = "Cannot delete";
                                }
                            }
                            else
                            {
                                lblThongbao.Text = "Not Existing Branches on Routes";
                            }
                        }
                        pnSearch.Visible = true;
                        break;

                }
            }
        }


    }
    protected void Button4_Click(object sender, EventArgs e)
    {
      
        pnCreate.Visible = true;
        pnSearch.Visible = false;
        pnEdit.Visible = false;

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        

        if (fn.CHECKEXIST("select * from BranchesOnRoute where  RouteID='" + ddlRoutes.SelectedValue + "' and Number='"+txtNumber.Text+"'") == true)
        {
            lblThongbao.Text = "Unsuccessfuly!Number existed on routes";

        }
        else {
            if (!fn.CHECKEXIST("select * from BranchesOnRoute where RouteID='' and Number=''"))
            {
                if (fn.EXECQUERY("insert into BranchesOnRoute values('" + ddlRoutes.SelectedValue + "','" + ddlBranches.SelectedValue + "','" + txtNumber.Text + "')") > 0)
                {
                    lblThongbao.Text = "Successfuly";
                }
                else
                {
                    lblThongbao.Text = "Unsuccessfuly! Cannot add item";
                }
            }
            else {
                lblThongbao.Text = "Exist number on routes";
            }
        
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        pnCreate.Visible = false;
        pnSearch.Visible = true;
        pnEdit.Visible = false;
        DataSet ds = fn.GETDATASET("select * from Routes order by StartPoint ", "routes");
        foreach (DataRow dr in ds.Tables["routes"].Rows)
        {
            ListItem l = new ListItem(dr["StartPoint"].ToString() + " -- " + dr["EndPoint"].ToString(), dr["RouteID"].ToString(), true);
            ddlRoutesSearch.Items.Add(l);
        }
    }

    protected void gvBranchesOnRoute_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        if (fn.CHECKEXIST("select * from BranchesOnRoute where RouteID='" + lblRoutID.Text + "' and Number='" + txtNumberEdit.Text + "'") != true)
        {
            string sql = "update BranchesOnRoute set Number='" + txtNumberEdit.Text + "' where RouteID='" + lblRoutID.Text + "' and BranchID='" + lblBrachID.Text + "'";
            if (fn.EXECQUERY(sql) > 0)
            {
                lblThongbao.Text = "Updated";
            }
            else
            {
                lblThongbao.Text = "Cannot update!";
            }
        }
        else {
            lblThongbao.Text = "Number existed ! cannot update";
        }
    }
}
